﻿using NPOI.SS.Formula.Functions;
using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Entity.Order;
using Shop.Model.Entity.Use;
using Shop.Model.View.Admin.Stats;

namespace Shop.Services.Admin;

public class StatsService:AbstractService, IStatsService
{
    private readonly RepositoryBase<UseUser> _useUserRepository;
    private readonly RepositoryBase<Orders> _ordersRepository;
    private readonly RepositoryBase<UserWallet> _walletRepository;
    private readonly RepositoryBase<UserWalletLog> _walletLogRepository;
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="useUserRepository"></param>
    /// <param name="ordersRepository"></param>
    /// <param name="walletRepository"></param>
    /// <param name="walletLogRepository"></param>
    public StatsService(RepositoryBase<UseUser> useUserRepository,
        RepositoryBase<Orders> ordersRepository,
        RepositoryBase<UserWallet> walletRepository,
        RepositoryBase<UserWalletLog> walletLogRepository)
    {
        _useUserRepository = useUserRepository;
        _ordersRepository = ordersRepository;
        _walletRepository = walletRepository;
        _walletLogRepository = walletLogRepository;
    }

    /// <summary>
    /// 用户人数统计
    /// </summary>
    /// <returns></returns>
    /// <exception cref="UserStatsVo"></exception>
    public AppResult<UserStatsVo> UserStats()
    {
        var userStatsVo = new UserStatsVo
        {
            SumUserCount = _useUserRepository.Where(x => true).Count(),
            DayUserCount = _useUserRepository.Where(x => x.CreatTime >= DateTime.Now.Date).Count(),
            SumAdvancedUserCount = _useUserRepository.Where(x => x.Grade == 3).Count()
        };
        var payUser = _ordersRepository.Where(x => x.OrderStatus >= 2).GroupBy(x => x.UserId). Select(x => new 
        {
            x.UserId
        }).ToList();
        userStatsVo.SumUserPayCount = payUser.Count();
        return userStatsVo;
    }

    /// <summary>
    /// 销售金额
    /// </summary>
    /// <returns></returns>
    /// <exception cref="MarketStatsVo"></exception>
    public AppResult<MarketStatsVo> MarketStats()
    {
        var marketStatsVo = new MarketStatsVo
        {
            SumMarket = _ordersRepository.Where(x => x.OrderStatus >= 2).Sum(x => x.OrderAmount),
            DayMarket = _ordersRepository.Where(x => x.OrderStatus >= 2 && x.PayTime >= DateTime.Now.Date)
                .Sum(x => x.OrderAmount),
            YesterdayMarket = _ordersRepository.Where(x => x.OrderStatus >= 2)
                .Where(x => x.PayTime >= DateTime.Now.Date.AddDays(-1) && x.PayTime < DateTime.Now.Date)
                .Sum(x => x.OrderAmount),
            MonthMarket = _ordersRepository.Where(x => x.OrderStatus >= 2)
                .Where(x => x.PayTime >= DateTime.Now.AddDays(1 - DateTime.Now.Day))
                .Sum(x => x.OrderAmount)
        };
        return marketStatsVo;
    }

    /// <summary>
    /// 佣金统计
    /// </summary>
    /// <returns></returns>
    /// <exception cref="BrokerageStatsVo"></exception>
    public AppResult<BrokerageStatsVo> BrokerageStats()
    {
        var brokerageStatsVo = new BrokerageStatsVo
        {
            SumBalance = _walletRepository.Where(x => true).Sum(x => x.Balance),
            ReceivablesBrokerage = _walletLogRepository.Where(x => x.Action == 3 && x.State == 0).Sum(x => x.Amount),
            WithdrawnBalance = _walletLogRepository.Where(x => x.Action == 2 && x.State == 1).Sum(x => x.Amount),
            DrawingBalance = _walletLogRepository.Where(x => x.Action == 2 && x.State == 0).Sum(x => x.Amount)
        };
        return brokerageStatsVo;
    }
}